package com.fasttreatment.zzr.demo.fragment;

import android.os.Bundle;
import android.view.View;

import com.android.androidlibrary.base.BaseFragment;
import com.fasttreatment.zzr.demo.R;
import com.fasttreatment.zzr.demo.myrouter.UrlRouter;
import com.fasttreatment.zzr.demo.adapter.ListAdapter;
import com.fasttreatment.zzr.demo.model.GankEntity;
import com.fasttreatment.zzr.demo.net.JsonCallback;
import com.fasttreatment.zzr.demo.net.NetApi;

import butterknife.BindView;
import cn.droidlover.qtcontentlayout.QTContentLayout;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * Created by zzr on 17/1/11.
 */

public class Test2Fragment extends BaseFragment {

    @BindView(R.id.rc_ContentLaYou)
    XRecyclerContentLayout rcContentlayouNew;
    protected static final int PAGE_SIZE = 10;
    protected static final int MAX_PAGE = 10;

    private ListAdapter mListAdapter;

    @Override
    public void initData(Bundle savedInstanceState) {
        loadData(1);
        initAdapter();

    }

    @Override
    public void setListener() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_test2;
    }

    public void initAdapter() {
        mListAdapter = new ListAdapter(context);
        rcContentlayouNew.getRecyclerView().verticalLayoutManager(context).setAdapter(mListAdapter);
        rcContentlayouNew.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        rcContentlayouNew.emptyView(View.inflate(getContext(),R.layout.view_empty,null));
        rcContentlayouNew.getRecyclerView().useDefLoadMoreView();
        rcContentlayouNew.getRecyclerView().setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                loadData(1);
            }

            @Override
            public void onLoadMore(int page) {
                loadData(page);

            }
        });
        mListAdapter.setRecItemClick(new RecyclerItemCallback<GankEntity.Item, ListAdapter.ViewHolder>() {
            @Override
            public void onItemClick(int position, GankEntity.Item model, int tag, ListAdapter.ViewHolder holder) {
                super.onItemClick(position, model, tag, holder);
                UrlRouter.goWeb(context,model.getUrl(),model.getDesc());
            }
        });

    }

    public void loadData(final int page) {
        NetApi.getGankData("Android", PAGE_SIZE, page, new JsonCallback<GankEntity>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                rcContentlayouNew.setDisplayState(QTContentLayout.STATE_ERROR);
            }

            @Override
            public void onResponse(GankEntity response, int id) {
                if (page > 1) {
                    mListAdapter.addData(response.getResults());
                } else {
                    mListAdapter.setData(response.getResults());
                }
                rcContentlayouNew.getRecyclerView().setPage(page,MAX_PAGE);
                if(mListAdapter.getItemCount()<1){
                    rcContentlayouNew.showEmpty();
                    return;
                }

            }
        });

    }

}
